package com.hertz.appr.service;

import java.net.URL;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Segment;
import net.htmlparser.jericho.Source;

import com.hertz.appr.dao.IBaseDao;
import com.hertz.appr.pojo.db.Random;

public class RandomService {

	private Logger log = Logger.getLogger(RandomService.class);
	private IBaseDao<Random, String> randomDao;
	/**
	 * <tr>
          <td align="center">2011-04-30</td>
          <td align="center">2011113</td>
          <td align="center" style="padding-left:20px;">
          <em>2</em>

          <em>9</em>
          <em>4</em>
          </td>

          <td align="center">16057</td>
          <td align="center">0</td>
          <td align="center">48701</td>

          <td><strong>61,491,762</strong></td>
          <td align="center"><a href="http://www.zhcw.com/3d/index.shtml?issue=2011113" target="_blank"><img src="http://images.zhcw.com/zhcw09/images/3dpd_ico1.gif" width="16" height="16" align="absmiddle" title="详细信息"/></a> 
          <a href="http://www.zhcw.com/video/zy_video_list_3d.html" target="_blank"><img src="http://images.zhcw.com/zhcw09/images/3dpd_ico2.gif" width="16" height="16" align="absmiddle" title="开奖视频"/></a></td>
        </tr>
	 * 从http://kaijiang.zhcw.com/zhcw/html/3d/list.html获取最新一期的中奖号，以及销售量
	 * 中奖号码 + 销售量 = random.num
	 * @return
	 */
	public Random getRandomFromZHCW(){
		Random random = null;
		String sourceUrlString = "http://kaijiang.zhcw.com/zhcw/html/3d/list.html";
		log.info("start get random from " + sourceUrlString);
		try {
			Source source = new Source(new URL(sourceUrlString));
			List<Element> tableList = source.getAllElements(HTMLElementName.TABLE);
			Element table = tableList.get(0);
			Segment tableSegContent = (Segment) table.getContent();
			List<Element> trList = tableSegContent.getAllElements(HTMLElementName.TR);
			Element firstTr = trList.get(2);
			Segment trSegContent = (Segment) firstTr.getContent();
			List<Element> tdList = trSegContent.getAllElements(HTMLElementName.TD);
			Element ymd = tdList.get(0);
			
			Element lotteryNumTd = tdList.get(2);
			String lotteryNumStr = "";
			List<Element> emList = lotteryNumTd.getAllElements(HTMLElementName.EM);
			for(Element el : emList){
				lotteryNumStr += el.getContent();
			}
			Element moneyTd = tdList.get(6);
			random = new Random();
			random.setYmd(ymd.getContent().toString());
			random.setCreateTime(System.currentTimeMillis());
			Long lotteryNum = Long.valueOf(lotteryNumStr);
			Long money = Long.valueOf(moneyTd.getContent().toString().replace("<strong>", "").replace("</strong>", "").replace(",", ""));
			Long num = money + lotteryNum;
			random.setNum(num);
			randomDao.save(random);
			log.info("save " + random);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			random = null;
		}
		return random;
	}
	/**
	 * 
	 * @param ymd yyyy-MM-dd
	 * @return
	 */
	public Random getRandom(String ymd){
		if(StringUtils.isBlank(ymd)){
			return null;
		}else{
			Random r = randomDao.getById(ymd);
			if(r == null){
				r = getRandomFromZHCW();
				if(r != null && r.getYmd().equals(ymd)){
					return r;
				}else{
					return null;
				}
			}else{
				return r;
			}
		}
	}
	public IBaseDao<Random, String> getRandomDao() {
		return randomDao;
	}
	public void setRandomDao(IBaseDao<Random, String> randomDao) {
		this.randomDao = randomDao;
	}
}
